package com.interviews.prodconsumer;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class MyResource {
    // 默认开始，进行生产和消费
    private volatile boolean FLAG = true;
    private AtomicInteger atomicInteger = new AtomicInteger();

    BlockingQueue<String> blockingQueue = null;

    public MyResource(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
        System.out.println(blockingQueue.getClass().getName());
    }

    public void myProd() throws Exception {
        boolean retValue = false;
        String data = "";
        while (FLAG) {
            data = atomicInteger.incrementAndGet() + "";
            retValue = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
            if (retValue) {
                System.out.println(Thread.currentThread().getName() + "\t 插入队列数据:" + data + "成功");
            } else {
                System.out.println(Thread.currentThread().getName() + "\t 插入队列数据:" + data + "失败");
            }
        }
        System.out.println(Thread.currentThread().getName() + "\t 大老板叫停了");

    }

    public void consumer() throws Exception {
        String result = null;
        while (FLAG) {
            result = blockingQueue.poll(2L, TimeUnit.SECONDS);
            if (null == result || result.equalsIgnoreCase("")) {
                FLAG = false;
                System.out.println(Thread.currentThread().getName() + "\t 超过2秒钟没有取到蛋糕，消费者退出");
                System.out.println();
                System.out.println();
                return;
            }
            System.out.println(Thread.currentThread().getName() + "\t 消费队列蛋糕" + result + "成功");
        }

    }

    public void stop() throws Exception {
        this.FLAG = false;
    }


}

/**
 * volatile/CAS/atomicInteger/BlockingQueue/线程交互/原子引用
 *
 * @author qian
 * @version 1.0
 * @date 2022/2/24 14:35
 */
public class BlockingQueueDemo {

    public static void main(String[] args) {
        MyResource myResource = new MyResource(new ArrayBlockingQueue<String>(100));

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t 生产线程启动");

            try {
                myResource.myProd();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, "produce").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t 消费线程启动");

            try {
                myResource.consumer();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, "consumer").start();

        try {
            // 暂停秒
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            myResource.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("5秒时间到，mian线程大老板叫停。活动结束！");

    }
}
